// @ts-nocheck
import { getInfo } from '@/services/user';

let _debounceTimeout = null,
	_throttleRunning = false;

/**
 * 防抖 
 */
export const debounce = (fn, delay = 500) => {
	clearTimeout(_debounceTimeout);
	_debounceTimeout = setTimeout(() => {
		fn();
	}, delay);
}

/**
 * 节流
 */
export const throttle = (fn, delay = 500) => {
	if (_throttleRunning) {
		return;
	}
	_throttleRunning = true;
	fn();
	setTimeout(() => {
		_throttleRunning = false;
	}, delay);
}

/**
 * 全局提示
 */
export const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

/**
 * 全局提示-错误
 */
export const errorMsg = (title, duration = 1500, mask = false) => {
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon: "error",
	});
}

/**
 * 全局提示-成功
 */
export const success = (title, duration = 1500, mask = false,) => {
	msg(title, duration, mask, "success");
}

/**
 * 全局提示-加载中
 */
export const showLoading = (title, mask = true) => {
	if (Boolean(title) === false) {
		title = "加载中...";
	}
	uni.showLoading({
		title: title
	})
}


/**
 * 常用输入判断
 */
export const checkTypeExp = (str, type) => {
	switch (type) {
		case 'mobile': //手机号码
			return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
		case 'tel': //座机
			return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
		case 'card': //身份证
			return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
		case 'mobileCode': //6位数字验证码
			return /^[0-9]{6}$/.test(str)
		case 'pwd': //密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线
			return /^([a-zA-Z0-9_]){6,18}$/.test(str)
		case 'payPwd': //支付密码 6位纯数字
			return /^[0-9]{6}$/.test(str)
		case 'postal': //邮政编码
			return /[1-9]\d{5}(?!\d)/.test(str);
		case 'QQ': //QQ号
			return /^[1-9][0-9]{4,9}$/.test(str);
		case 'email': //邮箱
			return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
		case 'money': //金额(小数点2位)
			return /^\d*(?:\.\d{0,2})?$/.test(str);
		case 'URL': //网址
			return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
		case 'IP': //IP
			return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
		case 'date': //日期时间
			return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) || /^(\d{4})\-(\d{2})\-(\d{2})$/
				.test(str)
		case 'number': //数字
			return /^[0-9]$/.test(str);
		case 'english': //英文
			return /^[a-zA-Z]+$/.test(str);
		case 'chinese': //中文
			return /^[\\u4E00-\\u9FA5]+$/.test(str);
		case 'lower': //小写
			return /^[a-z]+$/.test(str);
		case 'upper': //大写
			return /^[A-Z]+$/.test(str);
		case 'HTML': //HTML标记
			return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
		default:
			return true;
	}
}

/**
 * 订单状态列表
 */
export const orderStateList = [
	{ name: '已下单', type: '1' },
	{ name: '已打包', type: '3' },
	{ name: '已到达', type: '2' },
	{ name: '已退货', type: '4' },
];

/**
 * 设置不同种类的文字大小
 */
export const fontSizeItemBig = 38;
export const fontSizeItemTitle = 32;
export const fontSizeItemMessage = 24;
export const fontSizeItemMin = 16;
export const fontSizeItemMinMore = 21;
export const fontSizeItemSettingMessage = 28;

export function roundToDecimalPlace(number, decimalPlaces) {
  const factor = Math.pow(10, decimalPlaces);
  return Math.round(number * factor) / factor;
}

export const setTabBarSelected = (selected) => {
	const curPages = getCurrentPages()[0];
	if (typeof curPages?.getTabBar === 'function' && curPages?.getTabBar()) {
		curPages.getTabBar().setData({
			selected
		});
	}
};

export const setTabBarList = (isLogin) => {
	const curPages = getCurrentPages()[0];
	if (typeof curPages?.getTabBar === 'function' && curPages?.getTabBar()) {
		curPages.getTabBar().setData({
			list: isLogin ? tabBarLoginList : tabBarList,
		});
	}
};

export const tabBarList = [
	{
		"pagePath": "pages/index/index",
		"text": "首页",
		"iconPath": "/static/tab/home.png",
		"selectedIconPath": "/static/tab/home-active.png"
	},
	{
		"pagePath": "pages/shop/index",
		"text": "产品",
		"iconPath": "/static/tab/all-category.png",
		"selectedIconPath": "/static/tab/all-category-active.png"
	},
	{
		"pagePath": "pages/my/index",
		"text": "我的",
		"iconPath": "/static/tab/me.png",
		"selectedIconPath": "/static/tab/me-active.png"
	}
]

export const tabBarLoginList = [
	{
		"pagePath": "pages/index/index",
		"text": "首页",
		"iconPath": "/static/tab/home.png",
		"selectedIconPath": "/static/tab/home-active.png"
	},
	{
		"pagePath": "pages/shop/index",
		"text": "产品",
		"iconPath": "/static/tab/all-category.png",
		"selectedIconPath": "/static/tab/all-category-active.png"
	},
	{
		"pagePath": "pages/material/index",
		"text": "素材库",
		"iconPath": "/static/tab/material.png",
		"selectedIconPath": "/static/tab/material-active.png"
	},
	{
		"pagePath": "pages/my/index",
		"text": "我的",
		"iconPath": "/static/tab/me.png",
		"selectedIconPath": "/static/tab/me-active.png"
	}
]

export const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';

export const getUserInfo = async (store) => {
	store.commit('changeState', { loading: true })
	await getInfo().then((v: any) => {
		const { success, data } = v || {};
		if (success) {
			if (data.userId) {
				uni.setStorageSync('userId', data.userId);
			}
			store.commit('changeState', {
				userInfo: data,
				isLogin: data.nickName && data.headUrl,
				userId: data.userId,
				// isPoint: data.position !== 'vipCustomers',
				isPoint: true,
			})
		}
	}).finally(() => {
		store.commit('changeState', { loading: false })
	})
}

export const positionEnum = {
	vipCustomers: '贵宾顾客',
	internshipPartner: 'Q1经销商',
	juniorPartner: 'Q2经销商',
	intermediatePartner: 'M1合伙人',
	seniorPartner: 'M7合伙人',
	corePartner: '超级管理员',
};

export const materialTypeEnum = {
	usage: '产品搭配及使用方法',
	theoreticalKnowledge: '理论知识图案库',
	businessClass: '七巷里商学院课程',
	vipStory: '贵宾顾客故事会',
	entrepreneurship: '七巷里经销商创业会',
};

export const provinceList = [
	'江苏省', '浙江省', '上海市', '安徽省', '江西省', '福建省', '北京市',
	'天津市', '河北省', '湖南省', '山东省', '广东省', '湖北省', '河南省',
	'陕西省', '山西省', '四川省', '重庆市', '广西省', '贵州省', '吉林省',
	'辽宁省', '云南省', '黑龙江省', '内蒙古省', '青海省', '甘肃省', '宁夏省',
	'海南省', '新疆省', '西藏省'
]